<?php

namespace App\Http\Controllers\Clbum;




use App\Criteria\Clbum\Grade\GradeBaseQueryCriteria;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CurlRecordMethod;
use App\Http\Requests\Clbum\GradeBaseRequest;
use App\Libs\Tools\SystemCodeConstant;
use App\Repositories\Clbum\ClbumExtRepository;
use App\Repositories\Clbum\ClbumRepository;
use App\Repositories\Clbum\GradeRepository;
use App\Repositories\System\SystemCodeRepository;
use App\Repositories\System\UserRepository;
use App\Validators\Clbum\GradeValidator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Prettus\Validator\Contracts\ValidatorInterface;

class GradesController extends Controller
{

    use CurlRecordMethod;

    protected $index_page='clbum.grade.gradeMain';
    protected $add_page='clbum.grade.gradeAdd';
    protected $edit_page='clbum.grade.gradeEdit';

    protected $repository;
    protected $validator;
    protected $request;
    protected $userRepository;
    protected $clbumRepository;
    protected $systemCodeRepository;
    protected $clbumExtRepository;


    public function __construct(GradeRepository $repository,
                                GradeValidator $validator,
                                UserRepository $userRepository,
                                ClbumRepository $clbumRepository,
                                ClbumExtRepository $clbumExtRepository,
                                SystemCodeRepository $systemCodeRepository,
                                GradeBaseRequest $request)
    {
        $this->repository = $repository;
        $this->validator  = $validator;
        $this->request    = $request;
        $this->userRepository =$userRepository;
        $this->clbumRepository=$clbumRepository;
        $this->systemCodeRepository=$systemCodeRepository;
        $this->clbumExtRepository=$clbumExtRepository;
    }

    public function getBaseQueryCriteria(){
        return new GradeBaseQueryCriteria;
    }

    public function get_add_data(){
        $users=$this->userRepository->all();
        return view($this->add_page,compact('users'));
    }

    protected function post_add_data(){
        $all=$this->wrap_user_info($this->request->all(),true);
        try {
            $this->validator->with($all)->passesOrFail(ValidatorInterface::RULE_CREATE);
            if(!isset($all['header_teacher_user_ids']) || count($all['header_teacher_user_ids'])!=$all['clbum_cnt']){
                return $this->return_json_error_message("班主任数量必须与班级数量一致");
            }
            DB::transaction(function () use(&$all) {
                $model=$this->repository->skipPresenter()->create($all);
                $header_teacher_user_ids=$all['header_teacher_user_ids'];
                for ($clbum_cnt=1; $clbum_cnt<=intval($all['clbum_cnt']); $clbum_cnt++) {
                    $clbum_model=['name'=>'班级'.$clbum_cnt,'grade_id'=>$model->id,'header_teacher_user_id'=>$header_teacher_user_ids[$clbum_cnt-1]];
                    $clbum_model=$this->wrap_user_info($clbum_model,true);
                    $id = DB::table('clbum')->insertGetId($clbum_model);

                    $COURSE_LEADER=$this->create_clbum_exts($id,SystemCodeConstant::CLBUM_EXT_COURSE_LEADER_TYPE);
                    $COURSE_TEACHERS=$this->create_clbum_exts($id,SystemCodeConstant::CLBUM_EXT_COURSE_TEACHER_TYPE);
                    $STUDENT_LEADERS=$this->create_clbum_exts($id,SystemCodeConstant::CLBUM_EXT_STUDENT_LEADER_TYPE);

                    $this->save_clbum_exts($COURSE_LEADER);
                    $this->save_clbum_exts($COURSE_TEACHERS);
                    $this->save_clbum_exts($STUDENT_LEADERS);
                }
            });
            return $this->return_json_success();
        } catch (ValidatorException $e) {
            return $this->return_json_error($e);
        }
    }

    private function save_clbum_exts($clbum_exts){
        foreach ($clbum_exts as $key=>$clbum_ext){
            $this->clbumExtRepository->create($clbum_ext);
        }
    }


    private function create_clbum_exts($clbum_id,$type){
        $clbum_exts=[];
        $values=$this->systemCodeRepository->getSystemCodesByType($type);
        foreach ($values as $key=>$value){
            array_push($clbum_exts,[
                'clbum_id'=>$clbum_id,
                'system_code_type'=>$type,
                'system_code_value'=>$value->value,
                'created_user'=>Auth::user()['name'],
                'updated_user'=>Auth::user()['name'],
            ]);
        }
        return $clbum_exts;
    }
    public function get_edit_data(){
        $id=$this->request->get('id');
        $editEntity = $this->repository->skipPresenter()->find($id);
        $users=$this->userRepository->all();
        return view($this->edit_page,compact('editEntity','users'));
    }

}
